package com.company.ui;

import com.company.bean.Employee;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;

public class AddEmployeeFrame extends JFrame {
    private JTextField idField, nameField, ageField, positionField, salaryField, departmentField, phoneField;
    private JComboBox<String> genderComboBox;
    private JFormattedTextField joinDateField;
    private JButton submitButton;
    private JPanel addEmployeePanel;
    private EmployeeManagementFrame employeeManagement;

    public AddEmployeeFrame() {
        // 设置框架基本属性
        setTitle("添加员工信息");
        setSize(500, 600);
        setLocationRelativeTo(null); // 居中显示
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // 调用设置面板的方法
        setupPanel();
    }
    public AddEmployeeFrame(EmployeeManagementFrame employeeManagement){
        this();
        this.employeeManagement = employeeManagement;

    }

    // 设置面板内容
    private void setupPanel() {
        // 使用 GridBagLayout 布局管理器，使得面板自适应窗口大小
        setLayout(new BorderLayout());

        // 创建面板
        addEmployeePanel = new JPanel();
        addEmployeePanel.setLayout(new GridBagLayout());  // 使用 GridBagLayout
        addEmployeePanel.setBackground(new Color(245, 245, 245)); // 设置背景色
        add(this.addEmployeePanel);

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(10, 10, 10, 10); // 设置间距
        gbc.anchor = GridBagConstraints.WEST;

        // 标题
        JLabel titleLabel = new JLabel("添加员工信息");
        titleLabel.setFont(new Font("楷体", Font.BOLD, 20));
        titleLabel.setForeground(new Color(0, 123, 255)); // 设置字体颜色
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 2; // 跨越两列
        addEmployeePanel.add(titleLabel, gbc);

        // 员工ID标签和输入框
        JLabel idLabel = new JLabel("员工ID:");
        idField = new JTextField(20); // 增加宽度
        idLabel.setFont(new Font("楷体", Font.PLAIN, 14));
        idField.setFont(new Font("楷体", Font.PLAIN, 14));
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.gridwidth = 1; // 单列
        addEmployeePanel.add(idLabel, gbc);

        gbc.gridx = 1;
        addEmployeePanel.add(idField, gbc);

        // 姓名标签和输入框
        JLabel nameLabel = new JLabel("姓名:");
        nameField = new JTextField(20); // 增加宽度
        nameLabel.setFont(new Font("楷体", Font.PLAIN, 14));
        nameField.setFont(new Font("楷体", Font.PLAIN, 14));
        gbc.gridx = 0;
        gbc.gridy = 2;
        addEmployeePanel.add(nameLabel, gbc);

        gbc.gridx = 1;
        addEmployeePanel.add(nameField, gbc);

        // 性别标签和下拉框
        JLabel genderLabel = new JLabel("性别:");
        genderComboBox = new JComboBox<>(new String[] { "男", "女" });
        genderLabel.setFont(new Font("楷体", Font.PLAIN, 14));
        genderComboBox.setFont(new Font("楷体", Font.PLAIN, 14));
        gbc.gridx = 0;
        gbc.gridy = 3;
        addEmployeePanel.add(genderLabel, gbc);

        gbc.gridx = 1;
        addEmployeePanel.add(genderComboBox, gbc);

        // 年龄标签和输入框
        JLabel ageLabel = new JLabel("年龄:");
        ageField = new JTextField(20); // 增加宽度
        ageLabel.setFont(new Font("楷体", Font.PLAIN, 14));
        ageField.setFont(new Font("楷体", Font.PLAIN, 14));
        gbc.gridx = 0;
        gbc.gridy = 4;
        addEmployeePanel.add(ageLabel, gbc);

        gbc.gridx = 1;
        addEmployeePanel.add(ageField, gbc);

        // 职位标签和输入框
        JLabel positionLabel = new JLabel("职位:");
        positionField = new JTextField(20); // 增加宽度
        positionLabel.setFont(new Font("楷体", Font.PLAIN, 14));
        positionField.setFont(new Font("楷体", Font.PLAIN, 14));
        gbc.gridx = 0;
        gbc.gridy = 5;
        addEmployeePanel.add(positionLabel, gbc);

        gbc.gridx = 1;
        addEmployeePanel.add(positionField, gbc);

        // 入职时间标签和输入框
        JLabel joinDateLabel = new JLabel("入职时间:");
        joinDateField = new JFormattedTextField(new SimpleDateFormat("yyyy-MM-dd"));
        joinDateField.setColumns(20); // 增加宽度
        joinDateLabel.setFont(new Font("楷体", Font.PLAIN, 14));
        joinDateField.setFont(new Font("楷体", Font.PLAIN, 14));
        gbc.gridx = 0;
        gbc.gridy = 6;
        addEmployeePanel.add(joinDateLabel, gbc);

        gbc.gridx = 1;
        addEmployeePanel.add(joinDateField, gbc);

        // 薪水标签和输入框
        JLabel salaryLabel = new JLabel("薪水:");
        salaryField = new JTextField(20); // 增加宽度
        salaryLabel.setFont(new Font("楷体", Font.PLAIN, 14));
        salaryField.setFont(new Font("楷体", Font.PLAIN, 14));
        gbc.gridx = 0;
        gbc.gridy = 7;
        addEmployeePanel.add(salaryLabel, gbc);

        gbc.gridx = 1;
        addEmployeePanel.add(salaryField, gbc);

        // 部门标签和输入框
        JLabel departmentLabel = new JLabel("部门:");
        departmentField = new JTextField(20); // 增加宽度
        departmentLabel.setFont(new Font("楷体", Font.PLAIN, 14));
        departmentField.setFont(new Font("楷体", Font.PLAIN, 14));
        gbc.gridx = 0;
        gbc.gridy = 8;
        addEmployeePanel.add(departmentLabel, gbc);

        gbc.gridx = 1;
        addEmployeePanel.add(departmentField, gbc);

        // 电话标签和输入框
        JLabel phoneLabel = new JLabel("电话:");
        phoneField = new JTextField(20); // 增加宽度
        phoneLabel.setFont(new Font("楷体", Font.PLAIN, 14));
        phoneField.setFont(new Font("楷体", Font.PLAIN, 14));
        gbc.gridx = 0;
        gbc.gridy = 9;
        addEmployeePanel.add(phoneLabel, gbc);

        gbc.gridx = 1;
        addEmployeePanel.add(phoneField, gbc);

        // 提交按钮
        submitButton = new JButton("提交");
        submitButton.setFont(new Font("楷体", Font.BOLD, 14));
        submitButton.setBackground(new Color(0, 123, 255)); // 按钮背景色
        submitButton.setForeground(Color.WHITE); // 按钮文字颜色
        submitButton.setFocusPainted(false); // 去除焦点边框
        submitButton.setPreferredSize(new Dimension(120, 35)); // 设置按钮大小
        gbc.gridx = 0;
        gbc.gridy = 10;
        addEmployeePanel.add(submitButton, gbc);

        // 设置提交按钮事件
        submitButton.addActionListener(e -> {
            String idText = idField.getText();
            String name = nameField.getText();
            String gender = (String) genderComboBox.getSelectedItem();
            String ageText = ageField.getText();
            String position = positionField.getText();
            String joinDate = joinDateField.getText();
            String salaryText = salaryField.getText();
            String department = departmentField.getText();
            String phone = phoneField.getText();

            if (idText.isEmpty() || name.isEmpty() || ageText.isEmpty() || position.isEmpty() ||
                    joinDate.isEmpty() || salaryText.isEmpty() || department.isEmpty() || phone.isEmpty()) {
                JOptionPane.showMessageDialog(this, "所有字段都必须填写！", "提示", JOptionPane.WARNING_MESSAGE);
            } else {
                try {
                    int id = Integer.parseInt(idText);
                    int age = Integer.parseInt(ageText);
                    double salary = Double.parseDouble(salaryText);

                    Employee newEmployee = new Employee(id, name, gender, age, position, joinDate, salary, department, phone);
                    employeeManagement.addEmployee(newEmployee);
                    JOptionPane.showMessageDialog(this, "员工信息添加成功！", "成功", JOptionPane.INFORMATION_MESSAGE);
                    this.dispose();
                } catch (NumberFormatException ex) {
                    JOptionPane.showMessageDialog(this, "员工ID、年龄和薪水必须是有效的整数！", "错误", JOptionPane.ERROR_MESSAGE);
                }
            }
        });

        // 取消按钮
        JButton cancelButton = new JButton("取消");
        cancelButton.setFont(new Font("楷体", Font.BOLD, 14));
        cancelButton.setBackground(new Color(220, 53, 69)); // 按钮背景色
        cancelButton.setForeground(Color.WHITE); // 按钮文字颜色
        cancelButton.setFocusPainted(false); // 去除焦点边框
        cancelButton.setPreferredSize(new Dimension(120, 35)); // 设置按钮大小
        gbc.gridx = 1;
        gbc.gridy = 10;
        addEmployeePanel.add(cancelButton, gbc);

        // 设置取消按钮事件
        cancelButton.addActionListener(e -> {
            // 关闭当前窗口
            this.dispose();
        });
    }
}
